import wxPayUtil from "../../../wxpay/wxPayUtil";
var t = getApp(), e = t.requirejs("core"), o = t.requirejs("foxui");

Page({
  data: {
    icons: t.requirejs("icons"),
    success: !1,
    successData: {},
    coupon: !1
  },
  onLoad: function (e) {
    this.setData({
      options: e,
      imgUrl: t.globalData.approot
    }), t.url(e);
  },
  onShow: function () {
    this.get_list();
    //个人支付完成返回，开始处理数据
    var a = this;
    if (t.globalData.orderNo != null && t.globalData.payStatus) {
        let orderno = t.globalData.orderNo;
        console.log('接收到返回支付结果', t.globalData.payStatus);
        console.log('订单号', orderno);
        //成功业务处理
        // o.toast('成功业务处理');
        a.complete('wechat2');
      }
  },
  get_list: function () {
    var t = this;
    e.get("order/pay", t.data.options, function (o) {
      console.log('order/pay', o);
      //瞰云支付
      t.setData({
        list: o,
        show: !0
      })
      //以下是原来的
      //  50018 != o.error ? (!o.wechat.success && "0.00" != o.order.price && o.wechat.payinfo && e.alert(o.wechat.payinfo.message + "\n不能使用微信支付!"), 
      // t.setData({
      //   list: o,
      //   show: !0
      // })) : wx.navigateTo({
      //     url: "/pages/order/details/index?id=" + t.data.options.id
      // });
    });
  },
  pay: function (t) {
    var app = getApp();
    var a = this, s = e.pdata(t).type, a = this, i = this.data.list.wechat;
    o.toast(a, '马上支付，请勿重复点击...')
    e.post("order/pay/checkstock", {
      id: a.data.options.id
    }, function (t) {
      console.log('checkstock', t);
      console.log('e', e);
      console.log('a.data', a.data);
      //瞰云支付
      if ("wechat2" == s) {
        let out_trade_no = a.data.list.order.ordersn; //订单号
        let total_fee = a.data.list.order.price; //支付金额 单位：元
        let body = a.data.list.order.title; //商品简称
        let notify_url = app.globalData.approot +'payment/wechat/kanyun_notify.php'; //回调地址
        let attach = app.globalData.uniacid+":14"; // uniacid : 类型 。 14为小程序订单 具体见notify.php中定义
        let title = a.data.list.sysset.shopname//收银台标题
        let mchid = a.data.list.sysset.wxpay.mchid; //支付商户号
        let apikey = a.data.list.sysset.wxpay.apikey; //支付密钥
        wxPayUtil.toPay(mchid, apikey,out_trade_no, total_fee, body, notify_url, attach, title, (response) => {
          console.log(response);
        });
        return;
      }
      //瞰云支付 end
      //以下是原来的代码
      0 == t.error ? "wechat" == s ? e.pay(i.payinfo, function (t) {
        "requestPayment:ok" == t.errMsg && a.complete(s);
      }) : "credit" == s ? e.confirm("确认要支付吗?", function () {
        a.complete(s);
      }, function () { }) : "cash" == s ? e.confirm("确认要使用货到付款吗?", function () {
        a.complete(s);
      }, function () { }) : a.complete(s) : o.toast(a, t.message);
    }, !0, !0);
  },
  complete: function (t) {
    var a = this;
    e.post("order/pay/complete", {
      id: a.data.options.id,
      type: t
    }, function (t) {
      console.log('complete',t);
      if (0 != t.error) o.toast(a, t.message); else {
        wx.setNavigationBarTitle({
          title: "支付成功"
        });
        var e = Array.isArray(t.ordervirtual);
        a.setData({
          success: !0,
          successData: t,
          order: t.order,
          ordervirtual: t.ordervirtual,
          ordervirtualtype: e
        });
      }
    }, !0, !0);
  },
  shop: function (t) {
    0 == e.pdata(t).id ? this.setData({
      shop: 1
    }) : this.setData({
      shop: 0
    });
  },
  phone: function (t) {
    e.phone(t);
  },
  closecoupon: function () {
    this.setData({
      coupon: !1
    });
  }
});